package com.ruoyi.hospital.service.coupon.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDruginfo;
import com.ruoyi.hospital.mapper.storeInfo.PharmacyDruginfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.coupon.CouponInfoMapper;
import com.ruoyi.hospital.domain.coupon.CouponInfo;
import com.ruoyi.hospital.service.coupon.ICouponInfoService;

/**
 * 优惠券活动Service业务层处理
 *
 * @author wendao
 * @date 2024-09-11
 */
@Service
public class CouponInfoServiceImpl implements ICouponInfoService
{
    @Autowired
    private CouponInfoMapper couponInfoMapper;

    @Autowired
    private PharmacyDruginfoMapper pharmacyDruginfoMapper;

    /**
     * 查询优惠券活动
     *
     * @param id 优惠券活动主键
     * @return 优惠券活动
     */
    @Override
    public CouponInfo selectCouponInfoById(Integer id)
    {
        CouponInfo couponInfo = couponInfoMapper.selectCouponInfoById(id);
        //查询商品列表
        List<PharmacyDruginfo> products = new ArrayList<>();
        if(couponInfo.getUseScope()==1 && couponInfo.getProductIds()!=null){
            for (String s : couponInfo.getProductIds().split(",")) {
                products.add(pharmacyDruginfoMapper.selectPharmacyDruginfoById(Integer.valueOf(s)));
            }
        }
        couponInfo.setProductList(products);
        return couponInfo;
    }

    /**
     * 查询优惠券活动列表
     *
     * @param couponInfo 优惠券活动
     * @return 优惠券活动
     */
    @Override
    public List<CouponInfo> selectCouponInfoList(CouponInfo couponInfo)
    {
        List<CouponInfo> couponInfos = couponInfoMapper.selectCouponInfoList(couponInfo);
        //查询商品
        return couponInfos;
    }

    /**
     * 新增优惠券活动
     *
     * @param couponInfo 优惠券活动
     * @return 结果
     */
    @Override
    public int insertCouponInfo(CouponInfo couponInfo)
    {
        couponInfo.setCreateTime(DateUtils.getNowTimeSecond());
        return couponInfoMapper.insertCouponInfo(couponInfo);
    }

    /**
     * 修改优惠券活动
     *
     * @param couponInfo 优惠券活动
     * @return 结果
     */
    @Override
    public int updateCouponInfo(CouponInfo couponInfo)
    {
        couponInfo.setUpdateTime(DateUtils.getNowTimeSecond());
        return couponInfoMapper.updateCouponInfo(couponInfo);
    }

    /**
     * 批量删除优惠券活动
     *
     * @param ids 需要删除的优惠券活动主键
     * @return 结果
     */
    @Override
    public int deleteCouponInfoByIds(Integer[] ids)
    {
        return couponInfoMapper.deleteCouponInfoByIds(ids);
    }

    /**
     * 删除优惠券活动信息
     *
     * @param id 优惠券活动主键
     * @return 结果
     */
    @Override
    public int deleteCouponInfoById(Integer id)
    {
        return couponInfoMapper.deleteCouponInfoById(id);
    }
}
